package com.example.demo.controller;

import com.example.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author vivi
 * @description
 * @date 2024/5/26
 */
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController {
    //    @RequestMapping(value = "/show", method = RequestMethod.GET)
    @GetMapping("/show")
    public String show(String name) {
        return "hello!" + name;
    }

    @GetMapping("/num")
    public String getNum(Integer num) {
        return "num = " + num;
    }

    // 接受普通对象
    @GetMapping("/show-user")
    public String showUser(User user) {
        return user.toString();
    }
    // 接收json对象
    @PostMapping("/show-jsonuser")
    public String showJSONUSer(@RequestBody User user) {
        return user.toString();
    }

    @GetMapping("/show-time")
    //                                                表示该参数非必需
    public String showTime(@RequestParam(value = "t", required = false) String startTime,
                           @RequestParam("t2") String endTime) {
        return "开始时间：" + startTime + " 结束时间：" + endTime;
    }

    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable("username") String username,
                        @PathVariable("password") String password) {
        return username + " " + password;
    }

    @RequestMapping("/show/{username}/and/{password}")
    public String showInfo(@PathVariable("password") String pwd,
                           @PathVariable("username") String username) {
        return "用户名: " + username + " 密码: " + pwd;
    }

    // 上传文件
    @RequestMapping("/postfile")
    public String postFile(@RequestPart("myfile") MultipartFile file) throws IOException {
        String path = "E:\\105\\JAVAEE进阶\\springMVC-demo-new\\img.jpg";
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/myupfile")
    public String myUpFile(@RequestPart("myfile") MultipartFile file) throws IOException {
        String path = "E:\\105\\JAVAEE进阶\\springMVC-demo-new\\";
        path += UUID.randomUUID().toString().replace("-", "");
        // 加文件的后缀
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(path));
        return path;
    }

    @GetMapping("/getparam")
    public String getParam(HttpServletRequest req) {
        return req.getParameter("username");
    }

    @RequestMapping("/getcookie")
    public String getCookie(HttpServletRequest req) {
        Cookie[] cookies = req.getCookies();
        for (Cookie item: cookies) {
            log.error(item.getName() + ": " + item.getValue());
        }
        return "get cookie";
    }

    @RequestMapping("/getcookie2")
    public String getCookie2(@CookieValue("user1") String val) {
        return "Cookie Value: " + val;
    }

    @RequestMapping("/getuseragent")
    public String getUA(@RequestHeader("User-Agent") String userAgent) {
        return userAgent;
    }

    // 存 session 信息
    @RequestMapping("/setsession")
    public String setSession(HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.setAttribute("userinfo", "userinfo");
        return "Set Session Success.";
    }

    // 读 session 信息
    @RequestMapping("/getsession")
    public String getSession(HttpServletRequest req) {
        HttpSession session = req.getSession(false); // 不存在不需要重建
        if (session != null && session.getAttribute("userinfo") != null) {
            return (String) session.getAttribute("userinfo");
        }else {
            return "暂无 Session 信息";
        }
    }

    // 读取 Session 方式 2
    @RequestMapping("/getsession2")
    public String getSession2(@SessionAttribute(value = "userinfo", required = false) String info) {
        return info;
    }

    @RequestMapping("/respjson")
    public HashMap<String, String> respJson() {
        HashMap<String, String> map = new HashMap<>();
        map.put("Java", "Java Value");
        map.put("MySQL", "MySQL Value");
        map.put("Redis", "Redis Value");
        return map;

    }

}























